<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>动画</title>
  <style>
    .mycanvas{
      border: 2px solid black;
    }
  </style>
</head>
<body>
  <canvas class="mycanvas" width="1000px" height="800px"></canvas>
</body>

<script>
  var mycanvas = document.querySelector('.mycanvas');
  var ctx = mycanvas.getContext('2d');
  ctx.fillStyle = "green";

  var x = 50;
  setInterval(function(){
    //清除画布
    ctx.clearRect(0,0,mycanvas.width,mycanvas.height);
    ctx.fillRect(x,100,100,100);
    x+=2;
  },1000/50)

  var t = 50;
  (function run(){
    //ctx.clearRect(0,0,mycanvas.width,mycanvas.height);
    ctx.fillRect(t,200,100,100);
    t+=2;
    requestAnimationFrame(run);
  })();
</script>

</html>